﻿//Author:Michal Sporna
//License: https://operationsneakpeek.codeplex.com/license

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Util
{
    public static class MessageBox
    {
        #region properties
        public static bool ShowMessageBox;
        public static Rectangle MessageBoxRectangle;
        public static string Texture;
        public static List<string> Lines;

        public static bool IsLastMessageBox;
       
        #endregion

        #region PRIVATE
        private static Microsoft.Xna.Framework.Graphics.SpriteFont _Font;
 
        #endregion

        #region methods

        /// <summary>
        /// show the message box
        /// </summary>
        public static void ShowMessage(string message)
        {
            string[] MessageWords=null;
            string helpString=string.Empty;
            string futureString = string.Empty;
            int stringLenght=-1;
           

            try
            {

                MessageBoxRectangle = new Rectangle((Util.Cache.GraphicsDevice.Viewport.Width / 2) - 150, (Util.Cache.GraphicsDevice.Viewport.Height / 2) - 150, 300, 300);
                _Font = Util.Cache.GetFont("small").FontFace;

                //split message into words
               MessageWords = message.Split(new char[] { ' ' });

               if (Lines == null)
               {
                   Lines = new List<string>();
               }


               for (int i = 0; i < MessageWords.Length; i++)
               {
                    futureString += MessageWords[i]+" ";
                    stringLenght = (int)_Font.MeasureString(futureString).Length();

                    //if word fits the popup, then draw , otherwise increase Y and then draw
                    if (stringLenght > MessageBoxRectangle.Width-20)
                    {

                        Lines.Add(helpString);
                        helpString = MessageWords[i] + " ";
                        futureString = helpString;

                        continue;
                    }
                    else
                    {
                        helpString += MessageWords[i] + " ";
                    }
               }

                //there is only 1 line:
               if (helpString!=string.Empty)
               {
                   //add help string (will be added here if was not added above (case: string longer than box)
                   Lines.Add(helpString);
               }

                //Message = message;
                Texture = "MessageBox";
                ShowMessageBox = true;
                World.WorldManager.PauseON = true;

                MessageWords = null;
                helpString = null;

            }
            catch (Exception ex)
            {
                Util.ErrorLogging.LogError(ex);
                return;
            }
        }

        /// <summary>
        /// if message box is shown
        /// then game is paused and only OK button can close
        /// the message box and unpause the game
        /// //IN REALITY,TAPPING ANYWHERE ON THE POPUP CLOSES IT
        /// </summary>
        /// <param name="tapPos"></param>
        public static void OnOKButtonClicked(Vector2 tapPos)
        {
            try
            {
                if (ShowMessageBox && World.WorldManager.PauseON)
                {
                    if ((tapPos.X > MessageBoxRectangle.X && tapPos.X < MessageBoxRectangle.X + MessageBoxRectangle.Width) && (tapPos.Y > MessageBoxRectangle.Y && tapPos.Y < MessageBoxRectangle.Y + MessageBoxRectangle.Height))
                    {
                        MessageBoxRectangle = new Rectangle(-1, -1, -1, -1);
                        Texture = null;
                        Lines = null;
                        ShowMessageBox = false;
                        World.WorldManager.PauseON = false;
                        _Font = null;
                        IsLastMessageBox = false;
                    
                        return;
                    }
                }


            }
            catch (Exception ex)
            {
                Util.ErrorLogging.LogError(ex);
                return;
            }
        }

        /// <summary>
        /// draw the message box
        /// </summary>
        public static void Draw()
        {
          //declare
            int y = MessageBoxRectangle.Y + 20;
              
            

            try
            {
                if (ShowMessageBox)
                {
                    //draw messagebox
                    Util.Cache.Draw(Util.Cache.GetTexture(Texture).Text2D, MessageBoxRectangle, Color.White);

                    if (Lines != null)
                    {
                        foreach (string l in Lines)
                        {
                            Util.Cache.DrawString(_Font, l, new Vector2(MessageBoxRectangle.X+20, y), 25, Color.Black);
                            //increment y (to draw next line
                            y += 20;
                        }
                    }

                    //draw message
                    ////for (int i = 0; i < MessageWords.Length; i++)
                    ////{
                     

                    ////    //if word fits the popup, then draw , otherwise increase Y and then draw
                    ////    if (_Font.MeasureString(helpString).Length() > MessageBoxRectangle.X + MessageBoxRectangle.Width)
                    ////    {
                    ////        //word does not fit the box
                    ////        //increase Y
                    ////        Y += 20;
                    ////        //reset _X
                    ////        X = MessageBoxRectangle.X + 20;
                    ////        helpString = MessageWords[i];
                    ////    }

                    ////    Util.Cache.DrawString(_Font, "", new Vector2(X, Y), 25, Color.White);

                    
                    ////}

                }
               
            }
            catch (Exception ex)
            {
                Util.ErrorLogging.LogError(ex);
                return;
            }
        }


        #endregion




    }
}
